package com.wst.business.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wst.business.entity.SubTitleExtData;
import com.wst.system.config.exception.GlobalExcetion;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wst.system.util.SecurityUtil;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wst.business.entity.SubTitle;
import com.wst.business.mapper.SubTitleMapper;
import com.wst.business.service.SubTitleService;
import org.springframework.cache.annotation.*;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.beans.BeanUtils;
import com.wst.business.entity.vo.SubTitleVo;
import java.io.Serializable;
import java.util.ArrayList;

import com.wst.system.server.entity.vo.PageSearchVo;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author X.I.O
 * @since 2025-03-21
 */
@Service
@Transactional
public class SubTitleServiceImpl extends ServiceImpl<SubTitleMapper, SubTitle> implements SubTitleService {


    @Override
    public SubTitle getById(Serializable id){
        return super.getById(id);
    }


    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }


    @Override
    public boolean updateById(SubTitle subtitle) {
        return super.updateById(subtitle);
    }


    public SubTitle saveSubTitle(SubTitle subtitle) throws GlobalExcetion {
        super.save(subtitle);
        return subtitle;
    }


    public SubTitle updateSubTitleById(SubTitle subtitle) throws GlobalExcetion{
        super.updateById(subtitle);
        return subtitle;
    }


    public SubTitle saveOrUpdateSubTitle(SubTitleVo subtitleVo) throws GlobalExcetion{
        //Integer userId=SecurityUtil.getUserId();
        SubTitle subtitle=new SubTitle();
        BeanUtils.copyProperties(subtitleVo,subtitle);
        if(subtitleVo.getId()!=null){
            SubTitle subtitleEntity=super.baseMapper.selectById(subtitleVo.getId());
            if(subtitleEntity==null){
                throw new GlobalExcetion(1,"数据不存在");
            }
            super.baseMapper.updateById(subtitle);
        }else{
            SubTitleExtData subTitleExtData=new SubTitleExtData();
            //设置拓展数据
            subtitle.setExtData(JSONObject.toJSONString(subTitleExtData));
            if(subtitleVo.getImgList()==null){
                JSONArray array=new JSONArray();
                subtitle.setImgList(array.toJSONString());
            }
            //subtitle.setUid(userId.toString());
            super.baseMapper.insert(subtitle);
        }
        return subtitle;
    }

    public IPage<SubTitle> page(PageSearchVo vo) throws GlobalExcetion{
        LambdaQueryWrapper<SubTitle> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage<SubTitle> iPage=super.baseMapper.selectPage(new Page(vo.getPageNum(), vo.getPageSize()),lambdaQueryWrapper);
        return iPage;
    }
}